package org.zjx.app.task.tasks;


import org.zjx.app.commnad.ICommand;
import org.zjx.app.commnad.impl.ClaimTaskCommand;
import org.zjx.app.task.Task;
import org.zjx.app.task.TaskMatcher;
import org.zjx.app.task.TaskType;
import org.zjx.command.entity.Quest;
import org.zjx.command.state.QuestState;
import org.zjx.command.state.StateManager;
import org.zjx.data.GameSystem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CatchGhostsTask extends Task {

    public CatchGhostsTask() {
        super("常规任务", "捉鬼", TaskType.RANDOM);
        totalReps = 200;
        initReceiveNpc();
    }

    @Override
    public List<String> getTemplates() {
        List<String> strings = new ArrayList<>();
        strings.add("钟馗抓鬼（第{次数}个）：去{地址}附近抓{怪物名称}。");
        strings.add("钟馗抓鬼（第{次数}个）：去{地址}处抓{怪物名称}。");

        return strings;
    }

    public void initReceiveNpc() {
        npc = GameSystem.getInstance().getWorldManager().findNpc("钟馗","地府");
        dialogues = Collections.singletonList("好的，我帮你。");
    }

    private int matchTemplate(String input) {
        if (input.isEmpty()) {
            return -1;
        }
        for (int i = 0; i < templates.size(); i++) {
            String template = templates.get(i);
            TaskMatcher.MatchResult matchResult = TaskMatcher.matchTask(template, input);
            if (matchResult.isMatched()) {
                return i; // 返回匹配的模板索引（从0开始）
            }
        }
        return -1; // 无匹配
    }


    @Override
    public ICommand getCommand(StateManager stateManager) {
        QuestState questState = stateManager.getState(QuestState.class);
        Quest quest = containsQuest(new ArrayList<>(questState.getQuests().values()));
        if (quest == null) {
            return new ClaimTaskCommand(npc, dialogues);
        }

        int index = matchTemplate(quest.getContent());

//        //匹配错误,退出游戏
//        return new ExitCommand();
        return null;
    }

}
